package com.hbdl.web.sys.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.github.pagehelper.PageInfo;
import com.hbdl.common.base.ServiceMybatis;
import com.hbdl.common.mybatis.dao.MybatisDaoUtil;
import com.hbdl.common.utils.PMSUtils;
import com.hbdl.web.sys.model.Roles;
import com.hbdl.web.sys.model.User;
import com.hbdl.web.sys.utils.SysUserUtils;

/**
 * 
 * @description: 角色服务services
 * 
 * @version: 1.0
 * 
 * @create time: 2015-7-3
 * 
 * @author: ChenBo
 * 
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
@Service
public class SysRoleService extends ServiceMybatis<Roles> {

	private MybatisDaoUtil<Map> dao = new MybatisDaoUtil<Map>(Map.class);

	@Resource
	private SysResourceService sysResourceService;

	public PageInfo<Map> selectPage(Map params) {

		PageInfo page = dao.selectPage("rolesMapper.queryRole", params);
		return page;
	}

	public List selectRoleByCreateUser() {
		User user = SysUserUtils.getSessionLoginUser();
		List list = dao.selectList("rolesMapper.queryRole", new HashMap());

		return list;
	}

	public List getRoleResourceList(Map params) {

		User user = SysUserUtils.getSessionLoginUser();
		if ("admin".equals(user.getAccount()) && PMSUtils.isEmpty(params.get("role_uuid"))) {
			return sysResourceService.getResourceList(params);
		}

		return dao.selectList("rolesMapper.queryRoleResource", params);
	}

	public int save(Roles sysRole, Map params) {
		int count = 0;

		int role_uuid = 1;
		// 新增

		if (PMSUtils.isEmpty(role_uuid)) {
			count = this.insertSelective(sysRole);

		} else {
			this.updateByPrimaryKeySelective(sysRole);
			dao.delete("rolesMapper.deleteRoleResource", params);
		}
		return count;
	}

	public List getRoleByUser(Map params) {
		List list = dao.selectList("rolesMapper.getRoleByUser", params);

		return list;
	}

}